package com.whacos.common.utils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.whacos.common.entity.BaseEntity;
import com.whacos.common.vo.CommonVO;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * @author xiongdun
 * @description BeanUtils
 * @since 2018/12/8 23:58
 */
public class BeanUtils extends org.springframework.beans.BeanUtils {

    public static <T> List copyList(List<T> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList();
        }
        return JSON.parseArray(JSON.toJSONString(list), list.get(0).getClass());
    }

    public static Map<String, Object> copyMap(Map map) {
        return JSON.parseObject(JSON.toJSONString(map));
    }

    public static void copyPage(IPage source, IPage target) {
        BeanUtils.copyProperties(source, target);
        target.setSize(source.getSize());
        target.setTotal(source.getTotal());
        target.setCurrent(source.getCurrent());
        target.setPages(source.getPages());
    }

    /*public static <S extends BaseEntity, T extends CommonVO> void copyList(List<S> sourceSource, List<T> targetList) {
        if (ObjectUtils.isEmpty(sourceSource)) {
            return;
        }

        for (S s : sourceSource) {
            Consumer<T> t = T::new;
        }
    }*/
}
